<template>
  <div>
    <a-modal
      centered
      :title="title"
      :width="width"
      :visible="visible"
      @ok="handleOk"
      @cancel="handleCancel"
      cancelText="关闭">
      <a-spin :spinning="confirmLoading">
        <j-form-container :disabled="formDisabled">
          <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
            <a-row>
              <a-col :span="24">
                <a-form-model-item label="跟进类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
                  <a-select v-model="model.type" placeholder="请选择跟进类型" style="width: 100%">
                    <!--<a-select-option value="1">跟进记录</a-select-option>-->
                    <a-select-option value="2">微信跟进</a-select-option>
                    <a-select-option value="3">电话跟进</a-select-option>
                    <a-select-option value="4">面谈跟进</a-select-option>
                    <!--<a-select-option value="5">客户跟进</a-select-option>-->
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="实际跟进时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="followTime">
                  <j-date v-model="model.followTime" placeholder="请选择实际跟进时间" :disabled-date="disabledFollowTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="是否再访" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-radio-group default-value="1" v-model="model.isRevisit" button-style="solid">
                    <a-radio-button value="1">
                      否
                    </a-radio-button>
                    <a-radio-button value="2">
                      是
                    </a-radio-button>
                  </a-radio-group>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="跟进记录" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="content">
                  <a-textarea v-model="model.content" placeholder="请输入跟进记录" :autosize="{ minRows: 3, maxRows: 6 }"></a-textarea>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="下次跟进时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="nextFollowTime">
                  <j-date v-model="model.nextFollowTime" placeholder="请选择下次跟进时间" :disabled-date="disabledNextFollowTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="labourBeginTime">
                  <j-upload v-model="followImageList" text="上传" fileType="image"></j-upload>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="抄送人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ccListIds">
                  <a-select
                    mode="multiple"
                    style="width: 100%"
                    :filter-option="filterOptionCCList"
                    placeholder="请选择抄送人"
                    @change="changeCCList"
                  >
                    <a-select-option :value="item.id" v-for="(item,index) in this.ccListData" :key="index">
                      {{item.realname}}
                    </a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-form-model>
        </j-form-container>
      </a-spin>
    </a-modal>
  </div>
</template>

<script>
import Vue from "vue";
import { httpAction,getAction } from '@/api/manage'
import moment from 'moment'

export default {
  name: "addFollowLog",
  data() {
    return {
      followImageList: [],
      newFileList: [],
      width:'80%',
      model: {},
      title: '',
      visible: false,
      customerId: 0,
      labelCol: {
        xs: {span: 24},
        sm: {span: 5},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 16},
      },
      confirmLoading: false,
      validatorRules: {
        type: [{ required: true, message: '跟进类型不能为空' }],
        followTime: [{ required: true, message: '实际跟进时间不能为空' }],
        nextFollowTime: [{ required: true, message: '下次跟进时间不能为空' }],
        content: [{ required: true, message: '跟进记录不能为空' }],
      },
      url: {
        add: "/ghFollowRecord/add",
        queryUserList: "/sys/user/allList",
      },
      ccListData:[],    //抄送人列表
      ccListIds: "", //选择的抄送人id
      currentProgress: 0,
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
  },
  methods: {
    //实际跟进日期只能往前推两天
    disabledFollowTime(current) {
      // 限制选择今天以后的日期，今天不可选
      //return current && current <= moment().endOf('day');
      // 限制选择两天前，今天及今天以后的日期
      return current && current < moment().subtract(3, 'days').endOf('day')
    },
    // 限制选择今天及今后三天的日期
    disabledNextFollowTime(current){
      return current < moment().subtract(1, 'days').endOf('day') || current > moment().subtract(-3, 'days').endOf('day')
    },

    add(customerId) {
      this.customerId = customerId;
      this.visible = true;
      this.loadUserListData()
    },
    handleCancel() {
      this.visible = false;
    },
    handleOk() {
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          let oDate1 = new Date(this.model.followTime);
          let oDate2 = new Date(this.model.nextFollowTime);
          if(oDate1.getTime() >= oDate2.getTime()){
            this.$message.error("下次跟进时间不能小于实际跟进时间")
            return
          }
          this.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          }
          if (this.currentProgress != 0){
            this.model.progress = this.currentProgress;
          }
          if (this.followImageList.length > 0){
            this.model.images = this.followImageList.toString();
          }
          this.model.customerId = this.customerId;
          if (this.ccListIds != null && this.ccListIds != ""){
            this.model.ccList = this.ccListIds;
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              this.$message.success(res.message);
              this.model = {};
              this.$emit('ok');
            } else {
              this.$message.warning(res.message);
            }
          }).finally(() => {
            this.confirmLoading = false;
            this.visible = false;
            this.followImageList = [];
          })
        }
      })
    },
    changeCCList(value){
      this.ccListIds = value;
    },
    //加载用户列表
    loadUserListData() {
      getAction(this.url.queryUserList).then((res) => {
        if (res.success) {
          this.ccListData = res.result.records||res.result;
        }
      })
    },
    filterOptionCCList(input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      );
    },
  }
}
</script>

<style scoped>

</style>